<template>
  <div id='app'>
    <el-card>
      <div slot="header">
        <el-steps :active="activeStep" simple>
          <el-step v-for="(item, i) in steps"
           :title="item.title"
           :key="item.id"
           @click.native="activeStep = i"
           :icon="item.icon"></el-step>
        </el-steps>
      </div>
      <!-- 步骤条对应的表单 -->
      <el-form label-width="100px">
        <div v-show="activeStep === 0">
          <el-form-item label="课程名称">
            <el-input v-model="course.courseName"></el-input>
          </el-form-item>
          <el-form-item label="课程概述">
            <el-input style="width:45%;margin-right:20px" v-model="course.previewFirstField" placeholder="概述1">
              <template slot="append">{{ course.previewFirstField.length }}/ 50</template>
            </el-input>
            <el-input style="width:45%" v-model="course.previewSecondField" placeholder="概述2">
              <template slot="append">{{ course.previewSecondField.length }}/ 50</template>
            </el-input>
          </el-form-item>
          <el-form-item label="讲师姓名">
            <el-input v-model="course.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input v-model="course.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程排序">
            <el-input-number controls-position="right" v-model="course.sortNum" label="描述文字"></el-input-number>
          </el-form-item>
        </div>
        <div v-show="activeStep === 1">
          <course-img v-model="course.courseListImg" label="课程封面"></course-img>
          <course-img v-model="course.courseImgUrl" label="解锁封面"></course-img>
        </div>
        <div v-show="activeStep === 2">
          <el-form-item label="售卖价格">
            <el-input type="number" min="0" placeholder="请输入售卖价格" v-model="course.discounts">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input placeholder="请输入商品原价"  type="number" v-model="course.price">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input type="number" placeholder="请输入销量" v-model="course.sales">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input placeholder="请输入活动标签" v-model="course.discountsTag"></el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 3">
          <el-form-item label="限时秒杀开关">
            <el-switch v-model="course.activityCourse"
             active-color="#13ce66"
             inactive-color="#ff4949"
            ></el-switch>
          </el-form-item>
          <div v-if="course.activityCourse">
            <!--  id: 0,
          courseId: 0,
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0 -->
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="course.activityCourseDTO.beginTime"
                type="datetime"
                placeholder="选择日期时间"
                format="yyyy-MM-dd HH:mm:ss"
                default-time="12:00:00">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="course.activityCourseDTO.endTime"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间"
                default-time="12:00:00">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="秒杀价">
              <el-input placeholder="秒杀价" type="number" :min="0" v-model="course.activityCourseDTO.amount">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="库存">
              <el-input placeholder="库存" type="number" :min="0"  v-model="course.activityCourseDTO.stock">
                <template slot="append">个</template>
              </el-input>
            </el-form-item>
          </div>
        </div>
        <div v-show="activeStep === 4">
          <text-editor v-model="course.courseDescriptionMarkDown"></text-editor>
          <el-form-item label="是否上架">
            <el-switch
              v-model="course.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </div>
        <el-form-item>
          <el-button @click="activeStep--" v-if="activeStep != 0">上一步</el-button>
          <el-button @click="activeStep++" v-if="activeStep != 4">下一步</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="save" type="primary" v-if="activeStep === 4">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
import CourseImg from './courseImg.vue'
import TextEditor from '@/components/TextEditor'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: true
    },
    courseId: {
      type: [String, Number]
    }
  },
  components: {
    CourseImg,
    TextEditor
  },
  data () {
    return {
      // 进度
      activeStep: 0,
      // 进度条的相关信息
      steps: [
        { id: 1, title: '基本信息', icon: 'el-icon-edit' },
        { id: 2, title: '课程封面', icon: 'el-icon-upload' },
        { id: 3, title: '销售信息', icon: 'el-icon-picture' },
        { id: 4, title: '秒杀信息', icon: 'el-icon-picture' },
        { id: 5, title: '课程详情', icon: 'el-icon-picture' }
      ],
      isSeckill: false,
      // 添加课程的相关信息
      // 添加的时候,id相关的需要删除
      course: {
        // id: 0,
        courseName: '',
        brief: '',
        teacherDTO: {
          // id: 0,
          courseId: 0,
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '<h3>默认值</h3>',
        // 原价
        price: null,
        // 售卖价格
        discounts: null,
        priceTag: '',
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        // 课程封面地址
        courseListImg: '',
        // 解锁封面地址
        courseImgUrl: '',
        // 课程排序
        sortNum: 0,
        previewFirstField: '',
        previewSecondField: '',
        // 上架状态,0代表不上架,1代表上架
        status: 0,
        sales: 0,
        // 参与秒杀课程的数据
        activityCourse: false,
        activityCourseDTO: {
          // id: 0,
          courseId: 0,
          beginTime: '',
          endTime: '',
          amount: 0,
          stock: 0
        },
        autoOnlineTime: ''
      }
    }
  },
  methods: {
    async save () {
      const { data } = await saveOrUpdateCourse(this.course)
      console.log(data)
      if (data.code === '000000') {
        this.$message.success('添加成功')
        this.$router.push('/course')
      }
    },
    async loadCourse () {
      console.log('this.courseId', this.courseId)
      const { data } = await getCourseById(this.courseId)
      console.log(data)
      if (data.code === '000000') {
        this.course = data.data
        if (!this.course.activityCourse) {
          this.course.activityCourseDTO = {
            courseId: 0,
            beginTime: '',
            endTime: '',
            amount: 0,
            stock: 0
          }
        }
      }
    }
  },
  created () {
    // console.log('this.courseId', this.courseId, this.isEdit)
    if (this.courseId) {
      this.loadCourse()
    }
  },
  mounted () {}
}

</script>
<style scoped>
.el-step{
  cursor: pointer;
}
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
